using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using IGlobalist.Net.Rtp;

namespace IGlobalist.LectureTool.Controls
{
    public partial class ChatTab : TabPage
    {

        private IList<Image> emoticons = new List<Image>();

        private bool _isPublicTab = false;

        public bool IsPublicTab
        {
            get { return _isPublicTab; }
            set { _isPublicTab = value; }
        }

        private RtpSender sender;

        public RtpSender RtpSender
        {
            get { return sender; }
            set { sender = value; }
        }

        public ChatTab()
            : base()
        {
            InitializeComponent();
            LoadResources();
            
            txtMessageLog.LinkClicked += new LinkClickedEventHandler(txtMessageLog_LinkClicked);
        }

       public ChatTab(string text)
            : base(text)
        {
            InitializeComponent();
            LoadResources();
            txtMessageLog.LinkClicked += new LinkClickedEventHandler(txtMessageLog_LinkClicked);
        }

        void txtMessageLog_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            try
            {
                Uri uri = new Uri(e.LinkText);
                if (uri.Host == "sendfile.svc")
                {
                    string fName = e.LinkText.Split('=')[1];
                    //byte[] data = Global.eRoom.GetFileData(fName);
                    //if (data == null)
                    //    MessageBox.Show("Unable to download", "Error: Download error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //else
                    //{
                    //    dlgSaveFile.FileName = fName;
                    //    if (dlgSaveFile.ShowDialog(this) == DialogResult.OK)
                    //    {
                    //        System.IO.FileStream fs = new System.IO.FileStream
                    //        (dlgSaveFile.FileName, System.IO.FileMode.OpenOrCreate,
                    //         System.IO.FileAccess.Write,
                    //         System.IO.FileShare.None);
                    //        fs.Write(data, 0, data.Length);
                    //        fs.Close();
                    //        txtMessageLog.AppendTextAsRtf("********** File [" + fName + "] is saved ************");
                    //    }
                    //}
                }
            }
            catch
            {
                MessageBox.Show("Unable to download", "Error: Download error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadResources()
        {
            
            emoticons.Add(imageList.Images["AngelSmile.png"]);
            emoticons.Add(imageList.Images["AngrySmile.png"]);
            emoticons.Add(imageList.Images["Beer.png"]);
            emoticons.Add(imageList.Images["BrokenHeart.png"]);
            emoticons.Add(imageList.Images["ConfusedSmile.png"]);
            emoticons.Add(imageList.Images["CrySmile.png"]);
            emoticons.Add(imageList.Images["DevilSmile.png"]);
            emoticons.Add(imageList.Images["EmbarassedSmile.png"]);
            emoticons.Add(imageList.Images["ThumbsUp.png"]);
     
            foreach (Image icon in emoticons)
            {
                ToolStripButton mnui = new ToolStripButton("", icon,new EventHandler(mnui_Click));
                mnui.DisplayStyle = ToolStripItemDisplayStyle.Image;                
                btnDropIcons.DropDownItems.Add(mnui);                
            }
        }

        void mnui_Click(object sender, EventArgs e)
        {
            ToolStripButton mnui = (ToolStripButton)sender;
            txtMessage.InsertImage(mnui.Image);
        }
  

        public void SetRtf(string rtf)
        {
            try
            {
                txtMessageLog.AppendRtf(rtf);
            }
            catch
            {
                txtMessageLog.AppendTextAsRtf(rtf);
            }
            
            txtMessageLog.Focus();
            txtMessageLog.Select(txtMessageLog.TextLength, 0);
            txtMessageLog.ScrollToCaret();
            txtMessage.Focus();
        }
        public void SetText(string text, Font font, RtfColor color)
        {
            txtMessageLog.AppendTextAsRtf(text, font, color);
            txtMessageLog.Focus();
            txtMessageLog.Select(txtMessageLog.TextLength, 0);
            txtMessageLog.ScrollToCaret();
            txtMessage.Focus();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string rtf = txtMessage.Rtf.Replace("\r\n","");
            if (rtf.IndexOf("\\fs17{") != -1)
                rtf = rtf.Replace("\\fs17{", "\\fs17 " + Global.SourceDescription.Name + ": {");
            else
                rtf = rtf.Replace("\\fs17 ", "\\fs17 " + Global.SourceDescription.Name + ": ");
            
            txtMessage.Text = "";
            txtMessage.Focus();
            this.sender.Send(System.Text.Encoding.UTF8.GetBytes(rtf));

        }
        private void btnShowFontDlg_Click(object sender, EventArgs e)
        {
            try
            {
                if (dlgFont.ShowDialog(this) == DialogResult.OK)
                {
                    txtMessage.Font = dlgFont.Font;
                    txtMessage.ForeColor = dlgFont.Color;
                    txtMessage.TextColor = (RtfColor)Enum.Parse(txtMessage.TextColor.GetType(), "" + txtMessage.ForeColor.Name);
                }
            }
            catch { }
        }

        private void ChatTab_SizeChanged(object sender, EventArgs e)
        {
            txtMessage.Width = this.Width - btnSend.Width - 10;

            panel1.Width = txtMessageLog.Width = this.Width - 8;
            btnSend.Left = txtMessage.Width + 5;
        }

        private void txtMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                btnSend.PerformClick();
            }
        }  
    }
}
